package edu.utdallas_elearn_calendar.utdallas_elearn_calendar_app;

import edu.utdallas_elearn_calendar.utdallas_elearn_calendar_db.CalDbAdapter;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
//import android.widget.Toast;

public class univ_class_edit extends Activity {
	private EditText mPrefixText;
	private EditText mClassNumberText;
	private EditText mClassSectionText;	
	private EditText mClassTitleText;
	private EditText mClassSemesterText;
	private EditText mClassProfessorText;
	private EditText mClassTimeText;
	private EditText mClassLocationText;
	private Long mRowId;
	private CalDbAdapter mDbHelper;
	
	private static final int DELETE_ID = Menu.FIRST + 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		mDbHelper = new CalDbAdapter(this);
		mDbHelper.open();
        setContentView(R.layout.univ_class_edit);
		  
		mPrefixText = (EditText) findViewById(R.id.class_edit_prefix);
		mClassNumberText = (EditText) findViewById(R.id.class_edit_number);
		mClassSectionText = (EditText) findViewById(R.id.class_edit_section);		
		mClassTitleText = (EditText) findViewById(R.id.class_edit_title);
		mClassSemesterText = (EditText) findViewById(R.id.class_edit_semester);
		mClassProfessorText = (EditText) findViewById(R.id.class_edit_professor);
		mClassTimeText = (EditText) findViewById(R.id.class_edit_time);
		mClassLocationText = (EditText) findViewById(R.id.class_edit_location);

		mRowId = null;
		Bundle extras = getIntent().getExtras();
		mRowId = (savedInstanceState == null) ? null : (Long) savedInstanceState
				.getSerializable(CalDbAdapter.ROW_ID);
		
		if (extras != null) {
			mRowId = extras.getLong(CalDbAdapter.ROW_ID);
		}
		
		populateFields();
		Button saveButton = (Button) findViewById(R.id.class_edit_button);
		saveButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				setResult(RESULT_OK);
				finish();
			}
		});
		
		Button deleteButton = (Button) findViewById(R.id.class_delete_button);
		deleteButton.setOnCreateContextMenuListener(this);
    }
    
	private void populateFields() {
		if (mRowId != null && mRowId != 0) {
			Cursor currentRow = mDbHelper.getViewClass(mRowId);
			startManagingCursor(currentRow);
			
			mPrefixText.setText(currentRow.getString(currentRow
					.getColumnIndexOrThrow(CalDbAdapter.CLASS_PREFIX)));
			
			mClassNumberText.setText(currentRow.getString(currentRow
					.getColumnIndexOrThrow(CalDbAdapter.CLASS_NUMBER)));
			
			mClassSectionText.setText(currentRow.getString(currentRow
					.getColumnIndexOrThrow(CalDbAdapter.CLASS_SECTION)));			
			
			mClassTitleText.setText(currentRow.getString(currentRow
					.getColumnIndexOrThrow(CalDbAdapter.CLASS_TITLE)));
			
			mClassSemesterText.setText(currentRow.getString(currentRow
					.getColumnIndexOrThrow(CalDbAdapter.CLASS_SEMESTER)));
			
			mClassProfessorText.setText(currentRow.getString(currentRow
					.getColumnIndexOrThrow(CalDbAdapter.CLASS_PROFESSOR)));
			
			mClassTimeText.setText(currentRow.getString(currentRow
					.getColumnIndexOrThrow(CalDbAdapter.CLASS_TIME)));
			
			mClassLocationText.setText(currentRow.getString(currentRow
					.getColumnIndexOrThrow(CalDbAdapter.CLASS_LOCATION)));
			
			currentRow.close();
		}
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle(R.string.class_delete);
		
		// v.getId() was set by the button and is actually the row_id
		menu.add(mRowId.intValue(), DELETE_ID, 0, R.string.menu_delete);
		menu.add(0, DELETE_ID+1, 0, R.string.menu_cancel);
	}

	/**
	 * This method determines which button/class was selected
	 * this method cheats the MenuItem a little, instead of 
	 * using the GroupId to identify which group the menu 
	 * item is in we use it to store the row_id of the 
	 * class selected
	 */	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
//		Toast.makeText(this, "The id number of this class record is " + item.getGroupId() + ".", Toast.LENGTH_LONG).show();
		
		switch (item.getItemId()) {
		case DELETE_ID:
			mDbHelper.deleteClassDetail(item.getGroupId());
			mDbHelper.deleteClass(item.getGroupId());
			finish();
			return true;
		case DELETE_ID+1:
			return false;
		}

		return super.onContextItemSelected(item);
	}	
	
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		saveState();
		outState.putSerializable(CalDbAdapter.ROW_ID, mRowId);
	}

	@Override
	protected void onPause() {
		super.onPause();
		saveState();
		//mDbHelper.close();
	}

	@Override
	protected void onResume() {
		super.onResume();
		//mDbHelper.open();
		populateFields();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mDbHelper != null) {
			mDbHelper.close();
		}
	}

	private void saveState() {
		String prefix = mPrefixText.getText().toString();
		String class_number = mClassNumberText.getText().toString();
		String class_section = mClassSectionText.getText().toString();
		String title = mClassTitleText.getText().toString();
		String semester = mClassSemesterText.getText().toString();
		String professor = mClassProfessorText.getText().toString();
		String time = mClassTimeText.getText().toString();
		String location = mClassLocationText.getText().toString();
		

		// Insert a new or update an existing class
		if (mRowId == null || mRowId == 0) {
			
			long id = mDbHelper.insertClass(prefix, class_number, class_section, title);
			
			if (id > 0) {
				mRowId = id;
				id = mDbHelper.insertClassDetail(mRowId, semester, professor, time, location);
			}
		} else {
		
			mDbHelper.updateClass(mRowId, prefix, class_number, class_section, title);
			mDbHelper.updateClassDetail(mRowId, semester, professor, time, location);
		}
		
		mDbHelper.close();
		finish();
	}	
}